package com.edu.base.common.utils;

import it.sauronsoftware.jave.Encoder;
import it.sauronsoftware.jave.EncoderException;
import it.sauronsoftware.jave.InputFormatException;
import it.sauronsoftware.jave.MultimediaInfo;
import org.apache.commons.codec.digest.DigestUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.Map;

public class VideoUtil {
  private static Logger log = LoggerFactory.getLogger(VideoUtil.class);

  public static byte[] toByteArray(InputStream in) throws IOException {

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    byte[] buffer = new byte[1024 * 4];
    int n = 0;
    while ((n = in.read(buffer)) != -1) {
      out.write(buffer, 0, n);
    }
    return out.toByteArray();
  }

  /**
   * 网络文件转换为byte二进制
   *
   * @param @param  url
   * @param @return
   * @param @throws IOException 设定文件
   * @return byte[] 返回类型
   * @throws
   * @Title: toByteArray
   * @Description: TODO(这里用一句话描述这个方法的作用)
   */
  public static byte[] toByteArray(String urlStr) throws IOException {
    URL url = new URL(urlStr);
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    DataInputStream in = new DataInputStream(conn.getInputStream());
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    byte[] buffer = new byte[1024 * 4];
    int n = 0;
    while ((n = in.read(buffer)) != -1) {
      out.write(buffer, 0, n);
    }
    return out.toByteArray();
  }

  /**
   * @param @param  url
   * @param @return
   * @param @throws IOException 设定文件
   * @return byte[] 返回类型
   * @throws IOException
   * @throws MalformedURLException 网络文件转换为本地文件
   * @throws
   * @Title: toByteArray
   * @Description: TODO(这里用一句话描述这个方法的作用)
   */
  public static void toBDFile(String urlStr, String bdUrl)
      throws IOException, UnknownHostException {
    URL url = new URL(urlStr);
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    DataInputStream in = new DataInputStream(conn.getInputStream());
    byte[] data = toByteArray(in);
    in.close();
    FileOutputStream out = new FileOutputStream(bdUrl);
    out.write(data);
    out.close();
  }

  /**
   * 直接获取网络文件的md5值
   *
   * @param urlStr
   * @return
   */
  public static String getMd5ByUrl(String urlStr) {
    String md5 = null;
    try {
      URL url = new URL(urlStr);
      HttpURLConnection conn = (HttpURLConnection) url.openConnection();
      DataInputStream in = new DataInputStream(conn.getInputStream());
      md5 = DigestUtils.md5Hex(in);
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    return md5;
  }

  /**
   * 获取网络文件的输入流
   *
   * @param urlStr
   * @return
   */
  public static InputStream getInputStreamByUrl(String urlStr) {
    DataInputStream in = null;
    try {
      URL url = new URL(urlStr);
      HttpURLConnection conn = (HttpURLConnection) url.openConnection();
      in = new DataInputStream(conn.getInputStream());
    } catch (IOException e) {
      log.error("url转换输入流失败,错误信息{}", e.getMessage());
    }
    return in;
  }

  /**
   * 获取视频的基本信息(包括视频时长，视频宽高)
   *
   * @param url
   * @return
   * @throws IOException
   * @throws EncoderException
   * @throws InputFormatException
   */
  public static Map getVideoDetail(String url) throws Exception {
    File tmpFile = File.createTempFile("temp", ".tmp");//创建临时文件
    toBDFile(url, tmpFile.getCanonicalPath());
    MultimediaInfo m = new Encoder().getInfo(tmpFile);
    Map map = new HashMap();
    map.put("width", m.getVideo().getSize().getWidth());//获取视频宽高
    map.put("height", m.getVideo().getSize().getHeight());//获取视频长高
    map.put("duration", m.getDuration() / 1000);//获取视频时长（单位：秒）
    return map;
  }


}
